package com.setting.box

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.tools.box.setting.BuildConfig
import com.tools.box.setting.databinding.FragmentCommonSettingBinding

class CommonSettingFragment : Fragment() {

    private var isOpenPush: Boolean = false
    var bindingF: FragmentCommonSettingBinding? = null
    val binding get() = bindingF!!

    companion object {
        fun getInstance(): CommonSettingFragment {
            return CommonSettingFragment()
        }
    }

    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        bindingF = FragmentCommonSettingBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
    }

    private fun initView() {

        isOpenPush = com.setting.box.setting.util.SPUtil.with(requireContext()).load().read("isOpenPush", false)

        binding.titleTv.text = "设置"
        binding.backIv.setOnClickListener {
            requireActivity().finish()
//            if (activity != null && activity is CommonHomeActivity) {
//                (activity as CommonHomeActivity).showHome()
//            } else {
//                requireActivity().finish()
//            }
        }
        binding.userTv.setOnClickListener {
            val intent = Intent(requireActivity(), CommonXieYiActivity::class.java)
            intent.putExtra(CommonXieYiActivity.TITLE, "用户协议")
            intent.putExtra(CommonXieYiActivity.CONTENT, BuildConfig.USER_URL)
            requireActivity().startActivity(intent)
        }

        binding.provcyTv.setOnClickListener {
            val intent = Intent(requireActivity(), CommonXieYiActivity::class.java)
            intent.putExtra(CommonXieYiActivity.TITLE, "隐私政策")
            intent.putExtra(CommonXieYiActivity.CONTENT, BuildConfig.PRIVATE_URL)
            requireActivity().startActivity(intent)
        }

        binding.aboutTv.setOnClickListener {
            val intent = Intent(requireActivity(), CommonAboutActivity::class.java)
            requireActivity().startActivity(intent)
        }

        binding.feedbackTv.setOnClickListener {
            val intent = Intent(requireActivity(), CommonFeedBackActivity::class.java)
            requireActivity().startActivity(intent)
        }
        binding.pushSwitch.visibility = View.VISIBLE
        binding.pushSwitch.isChecked = isOpenPush
        binding.llpushSwitch.visibility = View.VISIBLE

        binding.pushSwitch.setOnCheckedChangeListener { buttonView, isChecked ->
            com.setting.box.setting.util.SPUtil.with(requireContext()).load().save("isOpenPush", isChecked)
            Toast.makeText(activity, "修改成功", Toast.LENGTH_SHORT).show()
        }

    }
}